/**
 * 
 * @Copyright (c) 2015 Messcat. All rights reserved.
 */
package cc.messcat.common.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.FilesUploadModel;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.common.util.FileHandler;
import cc.messcat.common.util.StringUtil;

/**
 * 公共接口 文件上传，文件下载，文件删除
 * 
 * @author Panda
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/base")
public class ActionController {

	@Value("#{ configProperties['web.name'] }")
	private String projectName;

	@Value("#{ configProperties['upload.image.path'] }")
	private String imageFolderName;

	@Value("#{ configProperties['upload.file.path'] }")
	private String fileFolderName;

	@Value("#{ configProperties['upload.file.maxUploadSize'] }")
	private Long fileMaxSize;

	@RequestMapping(value = "/imageUpload", method = RequestMethod.POST)
	@ResponseBody
	public Object pullImageFile(MultipartFile file) throws Exception {
		if (!file.isEmpty()) {
			FilesUploadModel fileModel = new FilesUploadModel(file.getOriginalFilename(), file.getInputStream(),
					projectName, imageFolderName);
			fileModel.writeFileByBinary();
			if (!fileModel.isFileExists()) {
				throw new Exception("文件写入异常！！");
			}
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_SUCCESS, fileModel.getFileUri());
		}else {
			return new ResponseBean(CommonConstant.FAIL_CODE_400,"文件不能为空");
		}
	}

	@RequestMapping(value = "/imageUploadByBase64", method = RequestMethod.POST)
	@ResponseBody
	public Object pullImageFileByBase64(@RequestParam String fileName, @RequestParam String fileContent) throws Exception {
		if (StringUtil.isNotBlank(fileName) && StringUtil.isNotBlank(fileContent)) {
			FilesUploadModel fileModel = new FilesUploadModel(fileName, fileContent, projectName, imageFolderName);
			fileModel.writeFileByBase64();
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_SUCCESS, fileModel.getFileUri());
		}else {
			return new ResponseBean(CommonConstant.FAIL_CODE_400,"文件不能为空");
		}
	}
/*
	@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
	@ResponseBody
	public Object pullFile(MultipartFile file) throws Exception {
		if (!file.isEmpty()) {
			FilesUploadModel fileModel = new FilesUploadModel(file.getOriginalFilename(), file.getInputStream(), projectName, fileFolderName);
			fileModel.writeFileByBinary();
			
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_SUCCESS, "abc");
		}else {
			return new ResponseBean(CommonConstant.FAIL_CODE_400,"文件不能为空");
		}
	}
*/
	
/*	@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
	@ResponseBody
	public Object pullFile(MultipartFile file) throws Exception {
		if (!file.isEmpty()) {
			FilesUploadModel fileModel = new FilesUploadModel(file.getOriginalFilename(), file.getInputStream(), projectName, fileFolderName);
			fileModel.writeFileByBinary();
			return  fileModel.getFileUri();
		}else {
			return  null;
		}
	}*/
	
	
/*	@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
//	@ResponseBody
	public void pullFile(MultipartFile file,HttpServletResponse response) throws Exception {
		if (!file.isEmpty()) {
			FilesUploadModel fileModel = new FilesUploadModel(file.getOriginalFilename(), file.getInputStream(), projectName, fileFolderName);
			fileModel.writeFileByBinary();
			response.setContentType("text/html;charset=UTF-8");
			String result = "{'message':'操作成功','status':'200','result':'"+fileModel.getFileUri()+"'}";
			response.getWriter().write(result);
//			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_SUCCESS, fileModel.getFileUri());
		}else {
			return ;
		}
	}
	*/
	
	/**
	 * 能兼用ie9上传方法
	 * http://blog.csdn.net/gao36951/article/details/46785323
	 * @param file
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
	public ResponseEntity<Map<String, String>> uploadPic(MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 设置响应类型为json并设置响应编码为utf-8
		Map<String, String> retMap = new HashMap<>();
		if (!file.isEmpty()) {
			FilesUploadModel fileModel = new FilesUploadModel(file.getOriginalFilename(), file.getInputStream(),
					projectName, fileFolderName);
			fileModel.writeFileByBinary();
			retMap.put("result", fileModel.getFileUri());
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.TEXT_PLAIN);
			return new ResponseEntity<Map<String, String>>(retMap, headers, HttpStatus.OK);
		} else {
			retMap.put("result", "");
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.TEXT_PLAIN);
			return new ResponseEntity<Map<String, String>>(retMap, headers, HttpStatus.BAD_REQUEST);
			
		}

	}
	
	
	
	
	@RequestMapping(value = "/fileRemove", method = RequestMethod.POST)
	@ResponseBody
	public Object deleteFile(String uri) throws Exception {
		FileHandler.deleteFile(uri, null, projectName);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_DELETE_SUCCESS);
	}

}
